package com.xyc.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xyc.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

//@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "9021000140662932";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCInCnE+QpJHMbJHkHDX9R37Jt5Cs28WkaTYMh5beGM+OGlRYOhJjb7OwWl4Z9e+rqjp/hR/IcKWHlPfMRIv2JSDQomoNGWFxoZy/4zpYNy2+5b+f1DPaE4sklKG0E6kSEZ3oM9dQmktHDUzRaCEIOoYj27zhAOkkQPODB3E4n/QpJUW5AZ1qfe1bvNz/juaFE6xmSRXbkGFW9djvd4Fm7W11hdkiU7gDe0ylf9GcIRDwMOmRInfQ7WvhEOSjDwMGvWiiY6JN7U/6bqDdz6M7ehBaCDNM/pELofKGWeUwXx84gPC1kEntyYWeIDRPDUWd+7WSvSZ9qyGfGZLtLxDNfNAgMBAAECggEAHDg2IKxZRZUeMzl+2D8ZgtRroWtr6Ha1kYgyoXLjXt2iryNW95kPToJ4vjWHWHVuzJPMJhOGScrQ9GyPl3+BbfWnnScnZFvNVnrfPgf4yWdJK9LOUG+QuEeMADT1GoYUUmW2sqFUpqNnk23yMlf6+nOg0QTWKExqpsIysMZmgUYP0zCvMfbc8AbgjI8qPqYvTO29r1wO8H3PDHtRe/9c1ppEMtS6Ud9vvOHBWfyi0n5ppPIzkAQ3rvYZYkeh9MC2fsTs58q4m6fU0EIsdYnCXJemy3sL83MBWUom+tZJVr8gKbnioftp3I7UycQC+4lPTZ1f4sMBZjNoVWzBrD19AQKBgQDGTKjLjgZD7XqCq+L0AZGOwApz/hIn/sQFUzFaISVdPzHoNRuVhYs7rPYlTSlpZILpgJ8OEvOfpkSc/YyzNqsxI6HwNv0UfmmoPIbUJNyNwvNeTl8ox3EOMfefeHgUmalQisu3gheoafsogh+Dt12myTMZeAqttF4NfIxGIm2abQKBgQCwXEDFyCw8oayZi4XyK5LC39f9fSNIev5fET3sKxfHsZt5+Go14M+gT/XQqgBd1Xt+lSCeuEygVRZ5KfJSCTGC3kJXR6dPYUfQKH9rUhpQXG0JdK1FvKCteLdK835bfA7vwcepJuQECZitKf65BoPTwuY96x67led8nz4oCYXW4QKBgDxAhLU81SiEmsnjglUTuO9/6G0wEMI4RcU8bu1sztaTYg2lPVlkN1cNicjxthiA4soiO7HiDkY/FfABlmPJw6BtrEG545H7iw0EPw9sl9s3Fg1ckMtqNi7S/eNljk7sFacUVSvQCnIwqZC4YOz7pSgNUEp3z/7RpBZC9rhEs7rNAoGAffJ0I4sv5GEFlWYp02fA2MDpTFOY5gNIM/ZpRYcE5Yz2XuXEXBPf0zonb8xolJqIJj8z24X82yfI8+C7+FAudqppsFEwsCTmF0dEsA0jU66eA/m6YKo0CY7DP5ii7YysUhcOksN+ZU/BJDFwgwyfCpUOvrtx6D9G4R4MUqVyWqECgYEAoy97d36tV194sfCu+RyJ0MmCS/lfMARJLtq+9glZZeQVI/mC4aMyy7DeH8U7TBAv336vgS8DtmZg+b0Ws9yMPnhggTZxJu5PdJPvS14b1/jiIq08i1STa8Mc8b2Ss1fikV/DcMTdO0Js+/mbvLKn6xorVcfUmxqxC+CFwbTWM2o=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlII3pHltbMnaUthn/y5Jw1cScwDRhNAiImvPsu0Mj78HVoO7kBdnDj8Wnj/S/UTx/y6UU77Ww7ZVqyahKYICMMzxR+ix5c8FtlZ2NFVO8pwck1urEmAKvuAqZR4MUPMzcS9kUrg5suKoTAxM1L4UpuJXHh9avVhQUvK7SXVYNRBIWOUybfqOgapPxLI+Gv7kowC4/GMhk5fXQ+Pou0AMgeWSjijxENTbJ+o0PB77KUtkuchtfj32zZZFhYEDiz3l17+cYTK8eTClIjh045RQsWOvjvxBFtX+mcsKhzyt3N451hRZ4Yp939JZow5v/MMs/eshwRZVu+ZxePlm95HSawIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "https://picked-moderately-albacore.ngrok-free.app/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //Alipay Client alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        // 2、创建一个支付请求 
        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
